﻿using Common;
using Common.XmlHandle;
using SFJDManagement.SocketHandle;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace SFJDManagement
{
    class Runtime
    {
        /// <summary>
        /// 鉴定程序使用机构的名称
        /// </summary>
        public static string institution = "陕西中正机动车物证司法鉴定所";
        /// <summary>
        /// 鉴定程序系统服务的名称
        /// </summary>
        public static string serviceName = "SFJDService";
        //public string pathConfig = "Config.xml";
        /// <summary>
        /// 程序安装路径
        /// </summary>
        public static string programPath = null;
        /// <summary>
        /// Temp文件夹路径
        /// </summary>
        public static string pathTemp = null;
        /// <summary>
        /// 本机用户信息
        /// </summary>
        public static ClassConfig config = null;
        /// <summary>
        /// 本机地址
        /// </summary>
        //public static IPAddress myIp = IPAddress.Parse("127.0.0.1");
        public static IPAddress ip = IPAddress.Parse(Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault<IPAddress>(a => a.AddressFamily.ToString().Equals("InterNetwork")).ToString());
        /// <summary>
        /// Socket消息处理字典
        /// </summary>
        public static Dictionary<int, Type> dictionary = new DictionaryInitialize().GetDictionary();
        /// <summary>
        /// Data.xml
        /// </summary>
        public static XmlData xmlData = null;
        /// <summary>
        /// XmlBrigade.xml
        /// </summary>
        public static XmlBrigade xmlBrigade = null;
        /// <summary>
        /// XmlContact.xml
        /// </summary>
        public static XmlContact xmlContact = null;
        /// <summary>
        /// Temp.xml
        /// </summary>
        public static XmlTemp xmlTemp = null;
        /// <summary>
        /// XmlUsersData.xml
        /// </summary>
        public static XmlUsersData xmlUsers = null;
        /// <summary>
        /// Config.xml
        /// </summary>
        public static XmlConfig xmlConfig = null;

        public static void Initialize()
        {
            RegistryHelper reg = new RegistryHelper();
            //programPath = reg.GetRegistryData("RootPath") + "\\";
            programPath = reg.GetRegistryData("RootPath");
            pathTemp = reg.GetRegistryData("TempPath");
            if (programPath == null || programPath == "")
            {
                programPath = Environment.CurrentDirectory;
            }
            string fileConfig = programPath + "\\Config.xml";
            string fileBrigade = programPath + "\\XmlBrigade.xml";
            string fileContact = programPath + "\\XmlContact.xml";
            string fileUsersData = programPath + "\\XmlUsersData.xml";
            string fileData = programPath + "\\Data.xml";
            string fileTemp = programPath + "\\Temp.xml";
            
            xmlData = new XmlData(fileData);
            xmlBrigade = new XmlBrigade(fileBrigade);
            xmlConfig = new XmlConfig(fileConfig);
            xmlContact = new XmlContact(fileContact);
            xmlUsers = new XmlUsersData(fileUsersData);
            xmlTemp = new XmlTemp(fileTemp);

            config = xmlConfig.GetConfig();
            if(config == null)
            {
                config = new ClassConfig();
                config.NewUser(Environment.UserDomainName);
            }
        }
    }
}
